{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.5"
    },
    "colab": {
      "name": "2aAND2b.ipynb",
      "provenance": [],
      "collapsed_sections": []
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "t3nFTfvm5WyE"
      },
      "source": [
        "import numpy as np\n",
        "from sklearn.linear_model import LogisticRegression\n",
        "from sklearn.metrics import accuracy_score\n",
        "\n",
        "#loading data\n",
        "Dsuper = np.loadtxt('superdata.txt')  \n",
        "Dunsuper = np.loadtxt('unsuperdata.txt')  \n",
        "Dtest = np.loadtxt('testdata.txt')  \n",
        "\n",
        "# defining global variables.\n",
        "Xtrain = Dsuper[:, :3] \n",
        "Ytrain = Dsuper[:, 3]  \n",
        "Xunsuper = Dunsuper  \n",
        "Xtest = Dtest[:,:3]    \n",
        "Ytest =  Dtest[:,3]    \n",
        "iterations = 20\n",
        "\n",
        "Model1 = LogisticRegression()# this model is trained using labelled dataset.(Model1)\n",
        "Model1.fit(Xtrain, Ytrain)\n",
        "\n",
        "# this model is trained using labelled and unlablled datasets. (Model2)\n",
        "while Xunsuper.size > 0:\n",
        "\n",
        "    Model2 = LogisticRegression()\n",
        "    Model2.fit(Xtrain, Ytrain)\n",
        "\n",
        "    probability = Model2.predict_proba(Xunsuper)\n",
        "    probBest = probability.max(axis=1)\n",
        "    max_i= sorted(range(len(Xunsuper)), reverse=True, key=lambda i: probBest[i])[:iterations]\n",
        "\n",
        "    Xtrain = np.vstack((Xtrain, Xunsuper[max_i])) # Stack arrays in sequence vertically (row wise).\n",
        "    var = probability.argmax(axis=1)\n",
        "    Ytrain = np.concatenate([Ytrain, var[max_i]]) \n",
        "    Xunsuper = np.delete(Xunsuper, max_i, axis=0)"
      ],
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BB0Kn-Uc5WyH",
        "outputId": "59e744c6-00c5-4ba4-c09c-ba7dc01d57db"
      },
      "source": [
        "m1 = Model1.score(Xtest, Ytest) #this model is trained using labelled dataset.(Model1)\n",
        "m2 = Model2.score(Xtest, Ytest) # this model is trained using labelled and unlablled datasets. (Model2)\n",
        "print(\"Model1: {0}%  \\nModel2: {1}%\".format(m1*100,m2*100))"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Model1: 100.0%  \n",
            "Model2: 100.0%\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-vA5mtYbAI9f"
      },
      "source": [
        "ref: https://machinelearningmastery.com/semi-supervised-learning-with-label-propagation/"
      ]
    }
  ]
}